//
//  SchedulingTableViewCell.m
//  HarmonizePub
//
//  Created by rzhy on 16/9/13.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "SchedulingTableViewCell.h"
#import "Config.h"
#import "SchedulingCollectionViewCell.h"

static NSString * CellIdentifier = @"Cell";
const CGFloat cellHeight = 66.0f;
const CGFloat itemCount = 3.0f;

@interface SchedulingTableViewCell () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *doctorNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation SchedulingTableViewCell

+ (NSString *)registerName {
    return @"SchedulingTableViewCell";
}

+ (CGFloat)cellHeightWithDataDictionary:(NSDictionary *)dataDict {
    NSInteger count = [dataDict[@"timeArray"] count];
    CGFloat height = (NSInteger)ceilf(count / itemCount) * [SchedulingCollectionViewCell itemHeight];
    height += cellHeight;
    if (height < cellHeight) height = cellHeight;
    return height;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.headImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.headImageView.layer.cornerRadius = 5.0f;
    self.headImageView.layer.masksToBounds = YES;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGFloat itemSize = XHScreenWidth / itemCount;
    flowLayout.itemSize = CGSizeMake(itemSize, [SchedulingCollectionViewCell itemHeight]);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    self.collectionView.collectionViewLayout = flowLayout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    UINib *nib = [UINib nibWithNibName:[SchedulingCollectionViewCell registerName] bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {}

- (void)setDataDict:(NSDictionary *)dataDict {
    _dataDict = dataDict;
//    XHLog(@"doctor: %@", doctor)
    [self setHeadImageWithImageString:dataDict[@"image"]];
    self.doctorNameLabel.text = dataDict[@"ygxm"];
    self.detailLabel.text = dataDict[@"description"];
    [self.collectionView reloadData];
}

- (void)setHeadImageWithImageString:(NSString *)string {
    [self.headImageView downloadImageWithURLString:string placeholderString:@"DoctorDefault.png"];
}

#pragma mark - collectionView delegate/dataSource Method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [_dataDict[@"timeArray"] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SchedulingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.dataDict = _dataDict[@"timeArray"][indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(schedulingCell:didSelectItemAtDictionary:timeDict:)])
        [self.delegate schedulingCell:self didSelectItemAtDictionary:self.dataDict timeDict:self.dataDict[@"timeArray"][indexPath.item]];
}

@end
